package iSOCK::Test::Setup;

use Digest::SHA1 qw( sha1_hex );
use iSOCK::Schema::App;

sub _login {
  return 'tester';
}

sub _password {
  return 'Bwaah!';
}

sub setup {
  my ( $self ) = @_;
  
  if ( my $schema = iSOCK::Schema::App->connect ({ dsn => $ENV{ISOCK_TEST_APP_DSN}, quote_names => 1 }) ) {
    my $id = _login ();
    
    $schema->resultset ( 'User' )->update_or_create ({ id => $id, name => 'Test Monkey', password => sha1_hex ( _password () ) });
    $schema->resultset ( 'Role' )->update_or_create ({ id => 'can_checkout_specification', description => 'can checkout from product subversion repository (specification, follow-up and integration)' });
    $schema->resultset ( 'UserToRole' )->update_or_create ({ user_id => $id, role_id => 'can_checkout_specification' });
    
    $schema->resultset ( 'RepositoryType' )->update_or_create ({ id => $_ }) for qw( subversion );
    $schema->resultset ( 'RepositoryContent' )->update_or_create ({ id => $_ }) for qw( specification );
    $schema->resultset ( 'Repository' )->update_or_create ({ id => $_, type_id => 'subversion', content_id => 'specification' }) for qw( product );
    $schema->resultset ( 'Specification' )->update_or_create ({ id => $_, repository_id => 'product' }) for qw( SAM9M11 );
    
    $schema->resultset ( 'BugTrackerType' )->update_or_create ({ id => 'bugzilla' });
    $schema->resultset ( 'ComponentType' )->update_or_create ({ id => $_, bug_tracker_type_id => 'bugzilla', bug_tracker_dsn => "dbi:SQLite:root/db/$_.db" }) for qw( ip analog digital memory );
    
    $schema->resultset ( 'Process' )->update_or_create ({ id => $_ }) for qw( at63900 );
    
    return 1;
  }
  
  return;
}

sub login {
  my ( $self, $mech ) = @_;
    
  $mech->get_ok ( '/login' );
  $mech->title_is ( 'iSOCK Login' );

  $mech->field ( user     => _login    () );
  $mech->field ( password => _password () );

  $mech->click;
  
  return $mech;
}

1;
